<template>
    <div>
        <van-button class="button" type="primary" @click="createAccounts">创建用户</van-button>
        <van-button class="button" type="primary" @click="getAccBalance">查看余额</van-button>
        <van-button class="button" type="primary" @click="sendTransaction">交易</van-button>

        <div>
            <p>地址：{{acc.address}}</p>
            <p>私钥: {{ acc.privateKey }}</p>
            <p>余额: {{ bal }}</p>
        </div>
    </div>
</template>
<script setup>
import {ref} from 'vue'
import Tx from "ethereumjs-tx";  
import Web3 from 'web3'

// "Web3.providers.givenProvider" will be set if in an Ethereum supported browser.
// wss://mainnet.infura.io/ws/v3/a87880aa11ba40b2bb8ed01b936b23f1
// wss://holesky.infura.io/ws/v3/a87880aa11ba40b2bb8ed01b936b23f1
var web3 = new Web3(Web3.givenProvider || 'wss://sepolia.infura.io/ws/v3/a87880aa11ba40b2bb8ed01b936b23f1');

var acc = {
    address: "0x9e2F77B6B6de16aB7E9a549E3C671FEB771EC20D",
    privateKey: "0xe341cc73e68eb0607bba5edd7bd1899e5806bc79e6bb7cf73ea1531ff824dd24",
    balance: 0
}

let bal = ref(0)

// 创建用户
function createAccounts(){
   
    let accounts = web3.eth.accounts.create('2435@#@#@±±±±!!!!678543213456764321§34567543213456785432134567');
    console.log("accounts:", accounts)
    return accounts
}
// 查看余额
function getAccBalance(){
    web3.eth.getBalance(acc.address).then(balance => {
        console.log(balance, web3.utils.fromWei(balance, 'ether'));
        bal.value = web3.utils.fromWei(balance, 'ether')
    })  
       
}
// 转账
async function sendTransaction(){
    // 这个是后期参数
    let sendInfo = {
        from_address: acc.address,
        privateKey: acc.privateKey,
        to_address:'0x03b655a06Df292e7bafaedE93B4eA8F910D25458',
        value: '0.001',

    }

    let nonce = await web3.eth.getTransactionCount(sendInfo.from_address);
    // 获取预计转账gas费
    let gasPrice = await web3.eth.getGasPrice();
    // 转账金额以wei为单位
    let balance = await web3.utils.toWei(sendInfo.value, 'ether');
    var rawTx = {
        from: sendInfo.from_address,
        nonce: Number(nonce),
        gasPrice: Number(gasPrice),
        to: sendInfo.to_address,
        value: Number(balance),
        data: "0x00", //转Token代币会用到的一个字段
    };

    
    // 将私钥去除“ox”后进行hex转化
    var privateKey = new Buffer.from(sendInfo.privateKey.slice(2), "hex");
    //需要将交易的数据进行预估gas计算，然后将gas值设置到数据参数中
    let gas = await web3.eth.estimateGas(rawTx);
    rawTx.gas = Number(gas);
    // 通过 ethereumjs-tx 实现私钥加密Ï
    var tx = new Tx(rawTx);
    tx.sign(privateKey);
    var serializedTx = tx.serialize();

    web3.eth.sendSignedTransaction("0x" + serializedTx.toString("hex"))
        .on("transactionHash", (txid) => {
            console.log("交易成功,请在区块链浏览器查看");
            console.log("交易id", txid);
            console.log(`https://goerli.etherscan.io/tx/${txid}`);

        }).on('receipt', (ret)=>{
            console.log("收据", ret)
        })
        // .on('confirmation', (ret)=>{
        //     console.log('确认节点',ret)
        // })
        .on("error", (err) => {
            console.log("error:" + err);
        });


}

</script>
<style>
.button{
    margin: 5px 10px;
}
</style>